package com.ang.hutool.DateUtil;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ssang
 * @date 2021-10-15 14:40
 */
public class DateDiff {
    public static void main(String[] args) {
        DateDiff diff = new DateDiff();
//        diff.diffDays();
//        diff.isWeekend();
//        diff.offSetDate();
//        diff.getLastMonth();
//        diff.getEveryMonthBetweenDate();
//        diff.compareDate();
//        diff.offsetMinute();
        beginOfMonth();
//        System.out.println(DateUtil.ageOfNow(DateUtil.parse("2021-10-27")));
    }

    public void diffDays(){
        //获取2个月前的日期
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH,-2);

        //获取日期差
        long diff = DateUtil.between(c.getTime(), new Date(), DateUnit.DAY);
        System.out.println(diff);
    }

    //是否周末
    public void isWeekend(){
        System.out.println(DateUtil.isWeekend(new Date()));
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE,3);
        System.out.println(DateUtil.isWeekend(c.getTime()));
    }

    //日期偏移量
    public void offSetDate(){
        String now = DateUtil.now();
        System.out.println(now);

        String today = DateUtil.today();
        System.out.println(today);

        DateTime day1 = DateUtil.offsetDay(new Date(),1);
//        System.out.println(day1);
        System.out.println(DateUtil.format(day1,"yyyy-MM-dd"));

        DateTime day2 = DateUtil.offsetDay(new Date(),2);
//        System.out.println(day2);
        System.out.println(DateUtil.format(day2,"yyyy-MM-dd"));

        //最后的参数如果是false，between的结果是day1-day2，如果是true，保留绝对值正数
        System.out.println(DateUtil.between(day1,day2,DateUnit.DAY,false));
    }

    //分钟的偏移量
    public void offsetMinute(){
        String start = "10:00";
        String end = "10:25";

        Date startDate = DateUtil.parse(start,"HH:mm");
        Date endDate = DateUtil.parse(end,"HH:mm");

        System.out.println(DateUtil.between(startDate, endDate, DateUnit.MINUTE, false));
    }

    /**
     * 获取上个月，起止日期
     */
    public static void getLastMonth(){
        //上个月的今天
        Date lastMonth = DateUtil.lastMonth();
        System.out.println(lastMonth.toString());

        //获取日期所在月的第一天
        DateTime beginTime = DateUtil.beginOfMonth(lastMonth);
        System.out.println(beginTime.toString());

        //获取日期所在月的最后一天
        DateTime endTime = DateUtil.endOfMonth(lastMonth);
        System.out.println(endTime.toString());
    }

    /**
     * 获取2个date之间，所有的月
     */
    public static void getEveryMonthBetweenDate(){
        String prefix = "date_";
        Date beginTime = DateUtil.parse("2018-02-09","yyyy-MM-dd");
        Date endTime = DateUtil.parse("2022-02-09","yyyy-MM-dd");
        //比较日期大小
        System.out.println(beginTime.before(endTime));
        System.out.println(beginTime.after(endTime));
        System.out.println(beginTime.equals(endTime));

        //获取开始时间所在月的第一天
        Date beginDate = DateUtil.beginOfMonth(beginTime);
        System.out.println(beginDate.toString());

        //获取截止时间所在月的第一天
        Date endDate = DateUtil.beginOfMonth(endTime);
        System.out.println(endDate.toString());

        System.out.println("-----------------------------------------------------------");

        List<Date> dateList = new ArrayList<>();
        while(beginDate.before(endDate) || beginDate.equals(endDate)){
            dateList.add(beginDate);
            //月份+1
            beginDate = DateUtil.offsetMonth(beginDate,1);
        }

        String result = dateList.stream().map(
                date -> prefix + DateUtil.format(date,"yyyy-MM-dd")
        ).collect(Collectors.joining(","));

        dateList.forEach(System.out::println);
        System.out.println(result);
    }

    /**
     * 比较日期大小
     */
    public static void compareDate(){
        Date date = DateUtil.parseDate("2022-05-10");

        System.out.println(DateUtil.compare(date, DateUtil.parseDate("2022-05-10")));
    }

    /**
     * 当天起始
     */
    public static void beginOfDay(){
        System.out.println(DateUtil.beginOfDay(new Date()));
    }

    /**
     * 当月起始
     */
    public static void beginOfMonth(){
        System.out.println(DateUtil.beginOfMonth(new Date()));
    }
}
